#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 5e3 + 10, M = N * 2;
int h[N], ne[M], e[M], w[M], idx;
int dis[N];
bool st[N];
int cnt[N];
int n, m;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
	memset(dis, 0x3f, sizeof(dis));
	queue<int>q;
	q.push(0);
	dis[0] = 0;
	st[0] = 1;
	while (q.size()) {
		int t = q.front();
		q.pop();
		st[t] = 0;
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (dis[j] > dis[t] + w[i]) {
				dis[j] = dis[t] + w[i];
				cnt[j] = cnt[t] + 1;
				if (!st[j]) {
					st[j] = 1;
					q.push(j);
				}
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++) {
		add(0, i, 0);
	}
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		add(b, a, c);
	}
	if (!spfa()) cout << "NO" << endl;
	for (int i = 1; i <= n; i++) {
		cout << dis[i] << " ";
	}
	return 0;
}